package timer;
import http.util.PrintUtil;
import main.robClass;
import mysql.MysqlUtil;
import user.IUser;

import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
//todo 需要修复不保存情况
public class saveCookieManager {

    /**
     * @param String username 用户登录名
     * @param Map<Long, List<Cookie>>  idAndCookies
     * id为 username+随机的uid组成
     * 根据id来获取cookie 某些场合抢课需要不同的cookies才能达到发包无限
     * 因此抢课需要根据id随机获取cookies进行发包
     */

    /**globalIdAndCookies - 用来记录所有用户的idAndCookies
    * 根据定时任务备份到每一位用户的数据库之中
    * 根据username 获取 IUser对象 从而方便 序列化cookies到指定的user -> mysqlUtil.updateUser(Iuser user)
    **/
    public static HashMap<String , HashMap <IUser, ArrayList<String>>> globalIdAndCookies = new HashMap<>();

    static {
        robClass.logger.info("saveCookieManager - 管理Cookie备份已加载");
    }

     /**
     * 开启定时备份cookie
     **/
    public static void updateUserToGlobalIdAndCookies(IUser user){
//        PrintUtil.printTitle("updateUserToGlobalIdAndCookies");
//        robClass.logger.info(user);
        String username = user.getUsername();
        if(user.getCookies()!=null&&user.getCookies().isEmpty()){
            robClass.logger.info(user.getUsername() + " 用户还没有取得一个可用Cookie");
            return;
        }
        HashMap <IUser,ArrayList<String>> idAndCookies = new HashMap<>();
//        robClass.logger.info("用戶的cookies:" + user.getCookies());
        idAndCookies.put(user,user.getCookies());
        boolean isFirstAdded = !globalIdAndCookies.containsKey(username);
        if(isFirstAdded){
            MysqlUtil.updateUser(user);
//            PrintUtil.printTitle( username+"已序列化到数据库之中" + user.toString());
        }
        globalIdAndCookies.put(username,idAndCookies);
//        PrintUtil.printTitle( username+" 已加入全局备份cookie数据中" + globalIdAndCookies);
    }


    /*
    *30秒备份一次
    **/
    public static void startBackupCookie(){
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        pool.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                //robClass.logger.info("globalIdAndCookies:" + globalIdAndCookies);
                PrintUtil.printTitle(globalIdAndCookies.entrySet().size()+"个#人员进行Cookie备份");
                if(globalIdAndCookies.isEmpty()){
                    PrintUtil.printTitle("沒有需要备份的人员");
                    return;
                }
                globalIdAndCookies.entrySet().parallelStream().forEach(
                        entry->
                                entry.getValue().entrySet().parallelStream().forEach(
                        idAndCookies->{
                            IUser user = idAndCookies.getKey();
                            MysqlUtil.updateUser(user);
//                            robClass.logger.info("为"+user.getUsername()+"进行Cookie数据备份");
                        }
                ) );
            }
        }, 10, 10, TimeUnit.SECONDS);
    }
}